﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Domain.Dto.AutoPushTimerDto>
@{
    ViewBag.subtitle = "推送时间段设置";
    ViewBag.title = "推送时间段设置";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{Html.Telerik().Grid(Model)
    .Name("AutoPushTimerGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "addautopushtimer"))
        { 
        <a href='javascript:;' class='t-button  t-grid-add'>添加时间段</a> 
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeleteautopushtimer"))
        {
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();

        columns.Bound(o => o.BeginHour).Width(50).Title("开始时间-小时");
        columns.Bound(o => o.BeninMinute).Width(50).Title("开始时间-分");
        columns.Bound(o => o.EndHour).Width(50).Title("结束时间-小时");
        columns.Bound(o => o.EndMinute).Width(50).Title("线束时间-分");

        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").ReadOnly();
        columns.Bound(o => o.AddTime).Width(200).Title("添加时间").ReadOnly();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();
        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "editautopushtimer"))
            {
                comm.Edit().ButtonType(GridButtonType.Text);
            }
            if (Html.HasUiPermission(this.ViewContext, "deleteautopushtimer"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(300).Title("删除");
    })
    .DataKeys(key => key.Add("Id"))//添加主键
    .ClientEvents(clientEvents => clientEvents.OnComplete("bindComplete"))
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxautopushtimerindex", "push")
        .Insert("addautopushtimer", "push")
        .Update("editautopushtimer", "push")
        .Delete("deleteautopushtimer", "push"))
    .Pageable(page => page.PageSize(15))
    .Render();
}
<script type="text/javascript">
    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/push/muldeleteautopushtimer', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#AutoPushTimerGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#AutoPushTimerGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
    });
</script>
